﻿Imports System.Security.Cryptography
Imports System.Text
Public Class m_usuarios
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub ImageButton1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click
        If txtnomb.Text = "" Or txtemaill.Text = "" Or txtuser.Text = "" Or txtcontrase.Text = "" Then
            Label1.Visible = True

            If txtnomb.Text = "" Then
                labelnomb.Visible = True
            Else
                labelnomb.Visible = False
            End If


            If txtcontrase.Text = "" Then
                labelpass.Visible = True
            Else
                labelpass.Visible = False

            End If

            If txtuser.Text = "" Then
                labeluser.Visible = True
            Else
                labeluser.Visible = False
            End If

            If txtemaill.Text = "" Then
                labelemail.Visible = True
            Else
                labelemail.Visible = False
            End If

        Else
            If Not txtemaill.Text.Contains("@") Then

                Label1.Visible = True
                labelemail.Visible = True
                Return
            Else
                Label1.Visible = False
                labelemail.Visible = False
            End If

            
            Try
                Dim x As String = generarClaveSHA1(txtcontrase.Text)
                txtcontrase.Text = Convert.ToString(x)

                Dim query As String = String.Format("insert into usuarios (usuarionombrecompleto , usuariotelefono , usuarioemail , usuariologin, usuariopass , empresaid , departamentoid , rangoid) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}')", txtnomb.Text, txtte.Text, txtemaill.Text, txtuser.Text, x, dropempresa.SelectedValue, dropdepto.SelectedValue, DropDownListtipouser.SelectedValue)
                Dim con As New Devart.Data.PostgreSql.PgSqlConnection(seguridad.conexion)
                con.Open()
                Dim ejec As New Devart.Data.PostgreSql.PgSqlCommand(query, con)
                ejec.ExecuteNonQuery()
                con.Close()

            Catch ex As Exception

            End Try


            txtcontrase.Text = ""
            txtemaill.Text = ""
            txtnomb.Text = ""
            txtte.Text = ""
            txtuser.Text = ""


        End If
    End Sub
    Private Function generarClaveSHA1(ByVal nombre As String) As String
        ' Crear una clave SHA1 como la generada por 
        ' FormsAuthentication.HashPasswordForStoringInConfigFile
        ' Adaptada del ejemplo de la ayuda en la descripción de SHA1 (Clase)
        Dim enc As New UTF8Encoding
        Dim data() As Byte = enc.GetBytes(nombre)
        Dim result() As Byte

        Dim sha As New SHA1CryptoServiceProvider
        ' This is one implementation of the abstract class SHA1.
        result = sha.ComputeHash(data)
        '
        ' Convertir los valores en hexadecimal
        ' cuando tiene una cifra hay que rellenarlo con cero
        ' para que siempre ocupen dos dígitos.
        Dim sb As New StringBuilder
        For i As Integer = 0 To result.Length - 1
            If result(i) < 16 Then
                sb.Append("0")
            End If
            sb.Append(result(i).ToString("x"))
        Next
        '
        Return sb.ToString.ToUpper
    End Function

    Protected Sub ImageButton6_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton6.Click
        Dim sUrl As String = "../Reportes/frmusuario.aspx"
        Dim sScript As String = "<script language =javascript> "
        sScript += "window.open('" & sUrl & "',null,'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=500,height=300,left=100,top=100');"
        sScript += "</script> "
        Response.Write(sScript)
        Dim repot As New rptusuario
        repot.UsuariosTableAdapter.Fill(repot.DataSet11.usuarios)

    End Sub
End Class